package com.company.park.tools;

import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.TagLibConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

/**
 * 开发调试使用的 Jetty Server
 * 
 * 
 */
public class JettyServer {

	/**
	 * @ProjectName: [cometdemo]
	 * @Package: [com.company.park.tools.JettyServer.java]
	 * @Description: [执行前请 mvn package一下]
	 * @Author: [jacarri]
	 * @CreateDate: [2014-3-24 上午9:30:11]
	 * @Version: [v1.0]
	 */
	public static void main(String[] args) throws Exception {
		Server server = buildNormalServer(8081, "/");
		server.start();
		// 打印启动调试信息
		// server.dump(System.err);
		server.join();
	}

	/**
	 * 创建用于正常运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
	 */
	public static Server buildNormalServer(int port, String contextPath) {
		Server server = new Server(port);
		// wardir使用src/main/webapp会导致servlet注解不会生效
		String wardir = "src/main/webapp";
		// String wardir = "target/cometdemo";
		WebAppContext webContext = new WebAppContext(wardir, contextPath);
		webContext.setDescriptor(wardir + "/WEB-INF/web.xml");
		webContext.setConfigurations(new Configuration[] {
				// new AnnotationConfiguration(),
				new WebXmlConfiguration(), new WebInfConfiguration(), new TagLibConfiguration(),
				new PlusConfiguration(), new MetaInfConfiguration(), new FragmentConfiguration(),
				new EnvConfiguration() });
		webContext.setClassLoader(Thread.currentThread().getContextClassLoader());
		server.setHandler(webContext);
		server.setStopAtShutdown(true);
		return server;
	}
}
